package com.neuedu.api.collection;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2816:50
 */
public class MyCollectionUtil {


    public static void main(String[] args) {
        List list = new ArrayList();

        list.add(new Integer(100));
        list.add(new Integer(900));
        list.add(new Integer(500));
        list.add(new Integer(600));
        list.add(new Integer(300));

        list.forEach(System.out::println);


        Collections.sort(list);    //list<?    Comparable>

        list.forEach(System.out::println);


        List<Pandan> pandaList  = new ArrayList();
        pandaList.add(new Pandan("熊猫1",18));
        pandaList.add(new Pandan("熊猫2",38));
        pandaList.add(new Pandan("熊猫3",28));
        pandaList.add(new Pandan("熊猫4",8));

        pandaList.forEach(System.out::println);
        Collections.sort(pandaList);    //list<?    Comparable>

        pandaList.forEach(System.out::println);
    }


}

class Pandan implements Comparable<Pandan>  {


    private String name;
    private Integer age;

    public Pandan(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Pandan other) {
        if(this.age >other.age){
            return 1;
        }else if(this.age <other.age){
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Pandan{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


